﻿using Catastrophe.BasePC.UI.DbDal;
using Catastrophe.BasePC.UI.Model;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Catastrophe.BasePC.UI.UIMode
{
    /// <summary>
    /// 菜单管理
    /// </summary>
    public partial class UcSysMenuModel : ObservableObject
    {
        /// <summary>
        /// 查询的节点
        /// </summary>
        [ObservableProperty]
        private string selectNode;

        /// <summary>
        /// 菜单信息
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<SysMenuItem> sysMenuEntities;

        /// <summary>
        /// 数据访问
        /// </summary>
        private SysMenuDal _baseMenuNodeDal { get; set; }

        /// <summary>
        /// 删除
        /// </summary>
        public ICommand delData { get; set; }
        /// <summary>
        /// 查询
        /// </summary>
        public ICommand selectData { get; set; }

        public UcSysMenuModel()
        {
            _baseMenuNodeDal = new SysMenuDal();
            sysMenuEntities = new ObservableCollection<SysMenuItem>();
            delData = new RelayCommand(delDataMethod);
            selectData = new RelayCommand(selectDataMethod);
            LoadMenu();
        }

        private void selectDataMethod()
        {
            LoadMenu();
        }

        private void delDataMethod()
        {
            foreach (var item in sysMenuEntities)
            {
                if (item.IsCheck)
                {
                    _baseMenuNodeDal.DeleteData(item.Id);
                }
            }
            LoadMenu();
        }

        /// <summary>
        /// 加载菜单
        /// </summary>
        public void LoadMenu()
        {
            sysMenuEntities.Clear();
            var menulist = _baseMenuNodeDal.QueryBaseMenuNodeList(SelectNode);
            var typebd = typeof(SysMenuEntity);
            var pros = typebd.GetProperties();
            var typeview = typeof(SysMenuItem);
            menulist.ForEach(x =>
            {
                SysMenuItem sysMenuItem = new SysMenuItem();
                sysMenuItem.MenuItemClile += SysMenuItem_MenuItemClile;
                foreach (var prop in pros)
                {
                    var viewpro = typeview.GetProperty(prop.Name);
                    viewpro.SetValue(sysMenuItem, prop.GetValue(x));
                }
                sysMenuEntities.Add(sysMenuItem);
            });
        }

        private void SysMenuItem_MenuItemClile(int obj)
        {
            foreach (var item in sysMenuEntities)
            {
                if (item.Id==obj)
                {
                    item.IsCheck = true;
                }
                else
                {
                    item.IsCheck = false;
                }
            }
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public partial class SysMenuItem : ObservableObject
    {
        /// <summary>
        /// 是否选中
        /// </summary>
        [ObservableProperty]
        private bool isCheck;

        /// <summary>
        /// 点击
        /// </summary>
        public ICommand Click { get; set; }

        public event Action<int> MenuItemClile;

        public SysMenuItem()
        {
            Click = new RelayCommand(ClickMethod);
        }

        private void ClickMethod()
        {
            MenuItemClile?.Invoke(Id);
        }

        [SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "id")]
        public int Id { get; set; }

        /// <summary>
        /// 节点编号
        /// </summary>
        [SugarColumn(ColumnName = "base_index")]
        public int BaseIndex { get; set; } = -1;

        /// <summary>
        ///父节点编号
        /// </summary>
        [SugarColumn(ColumnName = "parent_index")]
        public int ParentIndex { get; set; } = -1;

        /// <summary>
        /// 功能菜单
        /// </summary>
        [SugarColumn(ColumnName = "node_name")]
        public string NodeName { get; set; } = "";

        /// <summary>
        /// 可见
        /// </summary>
        [SugarColumn(ColumnName = "visible")]
        public bool Visible { get; set; }

        /// <summary>
        /// 固定到TAB
        /// </summary>
        [SugarColumn(ColumnName = "is_fixed")]
        public bool IsFixed { get; set; }
        /// <summary>
        /// 生命周期
        /// </summary>

        [SugarColumn(ColumnName = "instance_life")]
        public string InstanceLife { get; set; }
        /// <summary>
        /// 加载模式
        /// </summary>

        [SugarColumn(ColumnName = "load_mode")]
        public string LoadMode { get; set; }
        /// <summary>
        /// 窗口类型
        /// </summary>

        [SugarColumn(ColumnName = "form_type")]
        public string FormType { get; set; }

        [SugarColumn(ColumnName = "quick_access")]
        public bool QuickAccess { get; set; }

        [SugarColumn(ColumnName = "qaBtn_Index")]
        public int QaBtnIndex { get; set; }

        [SugarColumn(ColumnName = "quick_accessKey")]
        public string QuickAccessKey { get; set; }

        [SugarColumn(ColumnName = "fixed_frm_Index")]
        public int FixedFrmIndex { get; set; }

        [SugarColumn(ColumnName = "assembly")]
        public string Assembly { get; set; } = "";

        /// <summary>
        /// 资源地址
        /// </summary>
        [SugarColumn(ColumnName = "resource_name")]
        public string ResourceName { get; set; } = "";


        [SugarColumn(ColumnName = "wnd_child")]
        public string WndChild { get; set; }

        [SugarColumn(ColumnName = "load_prog_fullname")]
        public string LoadProgFullName { get; set; }

        [SugarColumn(ColumnName = "wnd_child_caption")]
        public string WndChildCaption { get; set; }

        [SugarColumn(ColumnName = "load_frm_size")]
        public string LoadFrmSize { get; set; }

        [SugarColumn(ColumnName = "before_show_method")]
        public string BeforeShowMethod { get; set; }

        [SugarColumn(ColumnName = "soft_id")]
        public string SoftId { get; set; }

        [SugarColumn(ColumnName = "time_stamp")]
        public string TimeStamp { get; set; }
    }
}
